using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Pavilion
{
    public class PlayerMouse : MouseCursor
    {
        public const string Name = "PlayerMouse";

        private bool leftButtonPressPending, rightButtonPressPending;

        public PlayerMouse() : base()
        {
            leftButtonPressPending = rightButtonPressPending = true;
        }

        public override void Update(GameTime gameTime)
        {
            //Update Position
            if (position.X != Mouse.GetState().X || position.Y != Mouse.GetState().Y)
            {
                position.X = Mouse.GetState().X;
                position.Y = Mouse.GetState().Y;

                OnMouseMoved();
            }

            //Update Scroller
            if (Mouse.GetState().ScrollWheelValue != scrollWheelValue)
            {
                scrollWheelRange = (Mouse.GetState().ScrollWheelValue - scrollWheelValue)/120;
                scrollWheelValue = Mouse.GetState().ScrollWheelValue;
                OnScrollWheelChanged();
            }

            //Update Buttons
            if (leftButtonPressPending)
            {
                if (ButtonState.Pressed == Mouse.GetState().LeftButton)
                {
                    OnLeftButtonPressed();
                    leftButtonPressPending = false;
                }
            }
            else
            {
                if (ButtonState.Released == Mouse.GetState().LeftButton)
                {
                    OnLeftButtonReleased();
                    leftButtonPressPending = true;
                }
            }

            if (rightButtonPressPending)
            {
                if (ButtonState.Pressed == Mouse.GetState().RightButton)
                {
                    OnRightButtonPressed();
                    rightButtonPressPending = false;
                }
            }
            else
            {
                if (ButtonState.Released == Mouse.GetState().RightButton)
                {
                    OnRightButtonReleased();
                    rightButtonPressPending = true;
                }
            }
        }
    }
}
